import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';


//编写两个正则表达式
final networkUriRef = RegExp('^http');
final localUriRef = RegExp('^static');

class CommonImage extends StatelessWidget {
  final String src;//图片地址
  final double width;
  final double height;
  final BoxFit fit;

  const CommonImage({Key key, this.src, this.width, this.height, this.fit}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    if(networkUriRef.hasMatch(src)){//验证正则是否匹配
      return ExtendedImage.network(
        src,
        width: width,
        height: height,
        fit: fit,
        cache: true,
        // image:ExtendedImage.network(
        //   src,
        //   useDiskCache: true,//是否使用缓存
        //   cacheRule: CacheRule(maxAge: Duration(days: 7)),//缓存规则，缓存7天
        //   timeoutDuration: Duration(seconds: 20)//超时时间
        // )
      );
    }
    if(localUriRef.hasMatch(src)){
      return Image.asset(src,width: width,height: height,fit: fit,);
    }
    //代码走到这会执行下面方法，如果为false就会给一个提示，提示内容为false后面的文字
    assert(false,"图片地址不合法");
    return Container();
  }
}
